package com.steam.jiaQiang;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author bin
 * @date 2025 - 04 -08
 **/
public class MainTest2 {
    public static void main(String[] args) {
        Method[] Fields = userService.class.getDeclaredMethods();
        Arrays.stream(Fields).filter(e -> e.isAnnotationPresent(ExceptionHandler.class))
                .sorted((o1,o2) ->
                    getLowestLevelExceptionDepth(o2.getAnnotation(ExceptionHandler.class).value()) -
                  getLowestLevelExceptionDepth(o1.getAnnotation(ExceptionHandler.class).value()))
                .forEach(e -> System.out.println(e.getName()));
    }

    private static int getInheritanceDepth(Class<?> clazz) {
        if (clazz == null || clazz == Object.class) {
            return 0;
        }
        return 1 + getInheritanceDepth(clazz.getSuperclass());
    }

    private static int getLowestLevelExceptionDepth(Class<?>[] exceptionClasses) {
        int maxDepth = 0;
        for (Class<?> exceptionClass : exceptionClasses) {
            int depth = getInheritanceDepth(exceptionClass);
            if (depth > maxDepth) {
                maxDepth = depth;
            }
        }
        return maxDepth;
    }
}
